import Axios from 'axios'

let ELE_MONKEY_SERVER_URL = {
  ADMINISTRATOR: 'http://10.222.29.161:8989',
  MERCHANT: 'http://10.222.29.195:8999',
  CUSTOMER: 'http://10.222.29.157:8080', // server at Burgess PC,
  BASE: 'http://10.222.29.157:8080' // common service server
}

let ELE_MONKEY_API = {
  UPLOAD_FILE: '/upload',
  RESOURCE: '/resource',
  ADMINISTRATOR: {
    USER_LOGIN: '/users/login',
    USER_LOGOUT: '/users/logout',
    GET_STORES_COUNT: '/stores/countStatus',
    GET_STORES: '/stores/status/',
    UPDATE_STORE: '/stores/',
    GET_ADS_COUNT: '/ads/countStatus',
    GET_ADS: '/ads/status/',
    UPDATE_AD: '/ads/',
    GET_COMPLAINS_COUNT: '/complains/countStatus',
    GET_COMPLAINS: '/complains/status/',
    UPDATE_COMPLAIN: '/complains/'
  },
  MERCHANT: {
    USER_REGISTER: '/user/regist',
    MERCHANT_APPLY: '/store/add',
    MERCHANT_USER_REG: '/user/regist',
    ORDER_ALLCOUNT: '/order/count/all/store',
    ORDER_ACTIVECOUNT: '/order/count/active/store',
    ORDER_NEWCOUNT: '/order/count/new/store',
    ORDER_ALL: '/order/store',
    USER_MSG: '/user',
    STORE_PRODUCTS: '/product/store',
    ORDER_ADD: '/order/add',
    STORE_MSG: '/store',
    STORE_UPDATESTATUS: '/order/update',
    PRODUCT_ADD: '/product/add',
    PRODUCT_UPDATE: '/product/update',
    PRODUCT_DELETE: '/product/delete',
    STORE_COMMENTS: '/comment/store',
    ORDER_COMMENT: '/comment/order',
    COMMENT_ADD: '/comment/add',
    STORE_UPDATE: '/store/update',
    AD_SEND: '/ad/send',
    USER_LOGIN: '/user/login',
    USER_UPDATE: '/user/update',
    STORE_BYUSERID: '/store/user'
  },
  CUSTOMER: {
    USER_LOGIN: '/login',
    USER_LOGOUT: '/logout',
    USER_REGISTER: '/register',
    USER_CHANGE_PWD: '/changePwd',
    ADVERTISEMENT: '/advertisement',
    STORE_HOT: '/store/hot',
    STORES: '/store',
    STORE: '/store',
    STORES_SID_PRODUCTS: '/prod',
    COMTACTS: '/contact',
    COMTACTS_DEL: '/contact/del',
    COMTACTS_UPD: '/contact/update',
    ORDERS_ACTIVE: '/order/user/active',
    ORDERS_HISTORY: '/order/user/history',
    COMPLAIN_ADD: '/complain',
    USER_UPD_LOGO: '/changeLogo'
  }
}

Axios.defaults.timeout = 5000
Axios.defaults.baseURL = ELE_MONKEY_SERVER_URL
Axios.defaults.withCredentials = true
Axios.defaults.headers = {
  'Content-Type': 'application/json'
}

let REQUEST_TIMEOUT = 0
let ERROR_CODE = {
  UNKNOWN_RESPONSE_CODE: -1,
  UNKNOWN_RESPONSE_DATA: -2,
  REQUEST_ERROR: -3,
  NETWORK_ERROR: -4
}
let MESSAGE_RESULT_CODE = {
  SUCCESS: 200,
  FAILED: 400,
  UNAUTHORIZED: 401,
  ERROR: 500
}

class BackParams {
  constructor (data, code, message) {
    this.data = data
    this.code = code
    this.message = message
  }
}

function getApiUrl (server, api) {
  return server + api
}

function getResourceUrl (res) {
  return ELE_MONKEY_SERVER_URL.BASE + ELE_MONKEY_API.RESOURCE + '/' + res
}

function request (params) {
  return new Promise((resolve, reject) => {
    setTimeout(function () {
      Axios({ method: params['method'], url: params['url'], data: params['data'] }).then((response) => {
        if (response === undefined || response['data'] === undefined) {
          reject(new BackParams(null, ERROR_CODE.NETWORK_ERROR, '网络异常'))
        } else if (response['data']['resultCode']) {
          switch (response['data']['resultCode']) {
            case MESSAGE_RESULT_CODE.SUCCESS:
            case MESSAGE_RESULT_CODE.FAILED:
            case MESSAGE_RESULT_CODE.UNAUTHORIZED:
            case MESSAGE_RESULT_CODE.ERROR:
              resolve(new BackParams(response['data']['content'], response['data']['resultCode'], response['data']['message']))
              break
            default:
              reject(new BackParams(response['data']['content'], ERROR_CODE.UNKNOWN_RESPONSE_CODE, '未知响应码'))
          }
        } else {
          reject(new BackParams(response['data'], ERROR_CODE.UNKNOWN_RESPONSE_DATA, '未知响应数据'))
        }
      }).catch((error) => {
        reject(new BackParams(error, ERROR_CODE.REQUEST_ERROR, '请求异常'))
      })
    }, REQUEST_TIMEOUT)
  })
}

export default {
  Axios,
  ELE_MONKEY_SERVER_URL,
  ELE_MONKEY_API,
  getApiUrl,
  getResourceUrl,
  REQUEST_TIMEOUT,
  ERROR_CODE,
  MESSAGE_RESULT_CODE,
  BackParams,
  request
}
